<?php
/**
 * (exporter)
 */
namespace App\Exporter\Purchase;

use App\Models\Product\ProductClassify;
use App\Models\Purchase\PurchaseOrder;
use App\Models\Purchase\PurchaseOrderDetail;
use App\Util\User;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithMapping;
class DetailExporter extends ExcelExporter implements WithMapping
{

    protected $fileName = '采购明细.csv';

    protected $headings = ['订单号','供应商','采购员','SKU','产品名称','季节性','品类','采购数量','采购单价',
        '采购金额','下单状态','交期','收货状态','已收货数量','结算方式','下单时间'];

    public function map($model) : array
    {
        return [
            $model->purchase_no,
            $model->purSupplier->name ?? '',
            User::getUsernameById($model->purchase->pu_user_id ?? -1),
            $model->sku->psku_code ?? '',
            $model->sku->psku_cn ?? '',
            ProductClassify::$_season[$model->skuClassify->season ?? 3],
            $model->skuClassify->title ?? '',
            $model->order_num ?: 0,
            $model->unit_price ?: 0,
            numberFormat(round(($model->order_num ?: 0) * ($model->unit_price ?: 0),3)),
            array_key_exists(($model->purchase->status ?? -1),PurchaseOrder::$_status) ? PurchaseOrder::$_status[$model->purchase->status] : '-',
            $model->purchase->commit_date ?? '',
            PurchaseOrderDetail::$_receivedStatus[$model->received_status],
            $model->rgoods_num,
            $model->purSettlementMethod->settlement_method ?? '',
            $model->purchase->order_at ?? '',
        ];
    }
}
